cmake_minimum_required(VERSION 3.14)
project (AIR[CXX])

set(AIR_CODE_DIR ${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

if (NOT BUILD_PATH)
    set(BUILD_PATH "${CMAKE_SOURCE_DIR}/build")
endif()

option(BUILD_OPEN_PROJECT "Enable air compile in opensource." FALSE)

if (BUILD_OPEN_PROJECT)
    if(DEFINED ENV{D_PKG_SERVER})
        set(AIR_PB_PKG $ENV{D_PKG_SERVER})
        message("Download packages from DPKG server${AIR_PB_PKG}")
    endif ()
    include(cmake/external_libs/protobuf_shared.cmake)
    include(cmake/external_libs/protobuf_static.cmake)
    include(cmake/external_libs/protoc.cmake)
    include(cmake/external_libs/securec.cmake)
    include(cmake/external_libs/json.cmake)
    include(cmake/FindModule.cmake)
    include(cmake/intf_pub_linux.cmake)

    if(DEFINED ENV{D_LINK_PATH})
        # D_LINK_PATH is set
        set(AIR_LIB_PATH $ENV{D_LINK_PATH})
        message("Link path is ${AIR_LIB_PATH}")
        set(AIR_SYS_ARCH "")
        if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
            # x86 ubuntu
            set(AIR_SYS_ARCH "x86_64")
        elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
            # arm euleros
            set(AIR_SYS_ARCH "aarch64")
        else()
            message(FATAL_ERROR "Running on a unsupported architecture: ${SYSTEM_TYPE}, build terminated")
        endif()
        set(AIR_LIB_PATH ${AIR_LIB_PATH}/${AIR_SYS_ARCH})
        message("Air lib path is ${AIR_LIB_PATH}")
        find_module(slog libalog.so ${AIR_LIB_PATH})
        find_module(mmpa libmmpa.so ${AIR_LIB_PATH})
        find_module(static_mmpa libmmpa.a ${AIR_LIB_PATH})
        find_module(compress libcompress.so ${AIR_LIB_PATH})
        find_module(platform libplatform.so ${AIR_LIB_PATH})
        if(PLATFORM STREQUAL "train")
            find_module(runtime libruntime.so ${AIR_LIB_PATH})
        elseif(PLATFORM STREQUAL "inference")
            find_module(runtime libruntime.so ${AIR_LIB_PATH})
            find_module(runtime_compile libruntime_compile.so ${AIR_LIB_PATH})
        elseif(PLATFORM STREQUAL "all")
            find_module(runtime libruntime.so ${AIR_LIB_PATH})
            find_module(runtime_compile libruntime_compile.so ${AIR_LIB_PATH})
        else()
            message(FATAL_ERROR "PLATFORM param is invalid, should be train or inference, build terminated")
        endif()
    else()
        if(DEFINED ENV{ASCEND_CUSTOM_PATH})
            set(ASCEND_DIR $ENV{ASCEND_CUSTOM_PATH})
            message("Ascend custom path is ${ASCEND_DIR}")
        else()
            set(ASCEND_DIR /usr/local/Ascend)
        endif()
        set(ASCEND_ACL_DIR ${ASCEND_DIR}/acllib/lib64)
        set(ASCEND_ATC_DIR ${ASCEND_DIR}/atc/lib64)
        set(ASCEND_RUNTIME_DIR ${ASCEND_DIR}/fwkacllib/lib64)
        set(ASCEND_DRIVER_DIR ${ASCEND_DIR}/driver/lib64)

        find_module(slog libalog.so ${ASCEND_ATC_DIR})
        find_module(mmpa libmmpa.so ${ASCEND_ATC_DIR})
        find_module(static_mmpa libmmpa.a ${ASCEND_ATC_DIR})
        find_module(compress libcompress.so ${ASCEND_ATC_DIR})
        find_module(platform libplatform.so ${ASCEND_ATC_DIR})
        if(PLATFORM STREQUAL "train")
            find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
        elseif(PLATFORM STREQUAL "inference")
            find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
        elseif(PLATFORM STREQUAL "all")
            find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
            find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
        else()
             message(FATAL_ERROR "PLATFORM ${PLATFORM} is invalid, should be train or inference, build terminated")
        endif()
    endif()

    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/third_party/metadef)
    set(AIR_ROOT_INC_DIR ${CMAKE_CURRENT_LIST_DIR}/third_party/inc)

    add_subdirectory(third_party/metadef)

    if (ENABLE_AICPU_LLT OR ENABLE_FE_LLT)
        include(cmake/external_libs/gtest.cmake)
        add_subdirectory(test)
    endif()
else()
    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/../metadef)
    set(AIR_ROOT_INC_DIR ${CMAKE_CURRENT_LIST_DIR}/../inc)
endif()

add_subdirectory(compiler)
